package com.ksck.logoDesign.app.network.request

import com.ald.plane_tool.config.Config
import com.ald.plane_tool.config.Config.REQUEST_TOKEN_INVALID
import com.blankj.utilcode.util.SPUtils
import okhttp3.Interceptor
import okhttp3.Response
import java.io.IOException
import kotlin.jvm.Throws


/**
 * Created by houzhanlei
 * 2020/7/24
 * 为自己代码负责
 * token 拦截器 做token更新
 */
class TokenInterceptor : Interceptor {

    @Throws(IOException::class)
    override fun intercept(chain: Interceptor.Chain): Response? {
        var request = chain.request()
        val response = chain.proceed(chain.request())
        if (response.code() === REQUEST_TOKEN_INVALID) { //401
            //TODO Token失效，刷新Token

        }else{
            request = request.newBuilder() //登录后将Token保存到本地
                .header(Config.HTTP_TOKEN_KET, SPUtils.getInstance().getString(Config.HTTP_TOKEN_KET))
                .build()
        }

        return chain.proceed(request)
    }

}

